import { observable, action } from 'mobx';
import api from '../../apis/app';
import { Toast } from 'weaver-mobile';
import { TIPS_MSG } from '../../constants/form';

export default class SetGroupPerson {
  @observable conversationId = '';
  @observable pkId = '';
  @observable goupPerson = [];
  @observable noWritePerson = [];
  @observable btnLoading = false;

  @action init = (pkId, conversationId) => {
    this.conversationId = conversationId;
    this.pkId = pkId;
    Toast.loading(TIPS_MSG.LOADING, 0);

    Promise.all([
      api.getConversationInfo({
        conversationId: conversationId,
        conversationType: 1,
        detail: 1
      }),
      api.getExceptMembers({ pkId: pkId })
    ]).then(([person, noWritePerson]) => {
      this.goupPerson = person.members;
      this.noWritePerson = noWritePerson;
      Toast.hide();
    }); 
  }

  @action onCheck = person => {
    const index = this.noWritePerson.indexOf(person.id);

    if (index >= 0) {
      this.noWritePerson.splice(index, 1);
    } else {
      this.noWritePerson.push(person.id);
    }
  }

  @action onSubmit = () => {
    this.btnLoading = true;

    return new Promise(resolve => {
      api.modifyExceptMembers({
        pkId: this.pkId,
        userIds: this.noWritePerson.join(',')
      }).then(res => {
        this.btnLoading = false;
        resolve();
      })
    });
  }

}